using System.Collections.Generic;
using GameFramework.Event;
using Newtonsoft.Json.Linq;
using UnityGameFramework.Runtime;

namespace StarForce.Hotfix
{
    public class ProcedureHotfixMain : ProcedureHotfixBase
    {
         /// <summary>
        /// 游戏模式字典
        /// </summary>
        private readonly Dictionary<GameMode, GameBase> m_Games = new Dictionary<GameMode, GameBase>();

        /// <summary>
        /// 当前的游戏
        /// </summary>
        private GameBase m_CurrentGame = null;

        /// <summary>
        /// 是否跳转到menu
        /// </summary>
        private bool m_GotoMenu = false;

        private bool m_Again = false;

        public void GotoMenu()
        {
            m_GotoMenu = true;
        }

        protected internal override void OnInit(IFsm procedureOwner)
        {
            base.OnInit(procedureOwner);
            m_Games.Add(GameMode.Normal, new NormalGame());
        }

        protected internal override void OnDestroy(IFsm procedureOwner)
        {
            base.OnDestroy(procedureOwner);
            m_Games.Clear();
        }

        private IFsm m_ProcedureOwner;
        protected internal override void OnEnter(IFsm procedureOwner)
        {
            base.OnEnter(procedureOwner);
            m_ProcedureOwner = procedureOwner;
            GameEntry.Base.FrameRate = 30;
            m_GotoMenu = false;
            GameMode gameMode = (GameMode) procedureOwner.GetData<VarInt>("GameMode").Value;
            m_CurrentGame = m_Games[gameMode];
            m_CurrentGame.Initialize();
        }
        
        protected internal override void OnLeave(IFsm procedureOwner, bool isShutdown)
        {
            base.OnLeave(procedureOwner, isShutdown);
            if (m_CurrentGame != null)
            {
                m_CurrentGame.Shutdown();
                m_CurrentGame = null;
            }
        }

        protected internal override void OnUpdate(IFsm procedureOwner, float elapseSeconds, float realElapseSeconds)
        {
            base.OnUpdate(procedureOwner, elapseSeconds, realElapseSeconds);
            if (m_CurrentGame != null && !m_CurrentGame.GameOver)
            {
                m_CurrentGame.Update(elapseSeconds, realElapseSeconds);
                return;
            }

            if (m_GotoMenu)
            {
                procedureOwner.SetData<VarInt>("NextSceneId",GameEntry.Config.GetInt("Scene.Menu"));
                ChangeState<ProcedureHotfixChangeScene>(procedureOwner);
            }
        }
    }
}